﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;
namespace Sale_Operation
{
    public class SaleBillOp : Component
    {
        public override bool Save(object obj)
        {
            throw new NotImplementedException();
        }
        public bool Save(object obj, DataTable dtDetail)
        {
            bool blRes = false;
            SaleBill SM = obj as SaleBill;
            int ioldId = SM.SBill_Id;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                if (SM.SBill_Id == 0)
                {
                    sbSql.Append("INSERT INTO SaleBill (SBill_Code,SBill_Code2,SBill_Cust_Id,SBill_TaxMoney,SBill_Money,SBill_RealMoney,SBill_PayType,SBill_AList_Id,SBill_Date,SBill_User,SBill_Type,SBill_State,SBill_Remark,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append("SBill_Code='" + ValueHandler.GetStringValue(SM.SBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Code2='" + ValueHandler.GetStringValue(SM.SBill_Code2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Cust_Id='" + ValueHandler.GetStringValue(SM.SBill_Cust_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_TaxMoney='" + ValueHandler.GetStringValue(SM.SBill_TaxMoney, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Money='" + ValueHandler.GetStringValue(SM.SBill_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_RealMoney='" + ValueHandler.GetStringValue(SM.SBill_RealMoney, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_PayType='" + ValueHandler.GetStringValue(SM.SBill_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_AList_Id='" + ValueHandler.GetStringValue(SM.SBill_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Date='" + ValueHandler.GetStringValue(SM.SBill_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_User='" + ValueHandler.GetStringValue(SM.SBill_User, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Type='" + ValueHandler.GetStringValue(SM.SBill_Type, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_State='" + ValueHandler.GetStringValue(SM.SBill_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Remark='" + ValueHandler.GetStringValue(SM.SBill_Remark, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("State=1,");
                    sbSql.Append("CreateTime=getdate(),");
                    sbSql.Append("CreateMan=" + LinkService.GetCurrentUser().SUser_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    SM.SBill_Id = ValueHandler.GetIntNumberValue(SqlHelper.ExecuteScalar(db.Transaction, CommandType.Text, "select @@identity "));
                }
                else
                {
                    sbSql.Append("UPDATE SaleBill SET ");
                    sbSql.Append("SBill_Code='" + ValueHandler.GetStringValue(SM.SBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Code2='" + ValueHandler.GetStringValue(SM.SBill_Code2, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Cust_Id='" + ValueHandler.GetStringValue(SM.SBill_Cust_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_TaxMoney='" + ValueHandler.GetStringValue(SM.SBill_TaxMoney, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Money='" + ValueHandler.GetStringValue(SM.SBill_Money, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_RealMoney='" + ValueHandler.GetStringValue(SM.SBill_RealMoney, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_PayType='" + ValueHandler.GetStringValue(SM.SBill_PayType, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_AList_Id='" + ValueHandler.GetStringValue(SM.SBill_AList_Id, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Date='" + ValueHandler.GetStringValue(SM.SBill_Date, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_User='" + ValueHandler.GetStringValue(SM.SBill_User, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Type='" + ValueHandler.GetStringValue(SM.SBill_Type, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_State='" + ValueHandler.GetStringValue(SM.SBill_State, ValueHandler.CharHandlerType.IsRepChar) + "',");
                    sbSql.Append("SBill_Remark='" + ValueHandler.GetStringValue(SM.SBill_Remark, ValueHandler.CharHandlerType.IsRepChar) + "' ");
                    sbSql.Append("WHERE SBill_Id=" + SM.SBill_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    //还原明细的库存
                    sbSql = new StringBuilder();
                    sbSql.Append(" SELECT SBDeta_Id  ,SBDeta_SBill_Id,SBDeta_PInfo_Id,SBDeta_Count,SBDeta_Price,SBDeta_Money,SBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM SaleBillDetail A ");
                    sbSql.Append(" LEFT JOIN ProductInfo B ON (A.SBDeta_PInfo_Id=B.PInfo_Id) ");
                    sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                    sbSql.Append(" WHERE A.SBDeta_SBill_Id=" + SM.SBill_Id + " AND A.State=1");
                    SqlHelper.FillDataset(db.Transaction, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                    for (int i = 0; i < db.Data.Tables[0].Rows.Count; i++)
                    {
                        sbSql = new StringBuilder();
                        sbSql.Append(" UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber+" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " , SDeta_OutputNumber=SDeta_OutputNumber-" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " WHERE SDeta_Stock_Id=1 AND SDeta_PInfo_Id=" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_PInfo_Id"]));
                        SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    }

                    //删除明细信息
                    sbSql = new StringBuilder();
                    sbSql.Append(" DELETE FROM SaleBillDetail WHERE SBDeta_SBill_Id=" + SM.SBill_Id);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());

                }
                //新增明细信息
                
                
                for (int i = 0; i < dtDetail.Rows.Count; i++)
                {
                    if (dtDetail.Rows[i].RowState == DataRowState.Deleted)
                        continue;
                    sbSql = new StringBuilder();
                    sbSql.Append(" INSERT INTO SaleBillDetail(SBDeta_SBill_Id,SBDeta_PInfo_Id,SBDeta_Count,SBDeta_Price,SBDeta_Money,SBDeta_Memo,State,CreateTime,CreateMan) SELECT ");
                    sbSql.Append(" SBDeta_SBill_Id=" + SM.SBill_Id + ",");
                    sbSql.Append(" SBDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_PInfo_Id"]) + ",");
                    sbSql.Append(" SBDeta_Count=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_Count"]) + ",");
                    sbSql.Append(" SBDeta_Price=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["SBDeta_Price"]).ToString() + ",");
                    sbSql.Append(" SBDeta_Money=" + ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["SBDeta_Money"]).ToString() + ",");
                    sbSql.Append(" SBDeta_Memo='" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_Memo"], ValueHandler.CharHandlerType.IsRepChar).ToString() + "',");
                    sbSql.Append(" State=1,");
                    sbSql.Append(" CreateTime=getdate(),");
                    sbSql.Append(" CreateMan=" + SM.CreateMan);
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                    //更新库存
                    sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber-" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_Count"]) + ", SDeta_OutputNumber=SDeta_OutputNumber+" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_Count"]) + " WHERE SDeta_Stock_Id=1 AND SDeta_PInfo_Id=" + ValueHandler.GetStringValue(dtDetail.Rows[i]["SBDeta_PInfo_Id"]));
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());

                }
                db.Transaction.Commit();
                blRes = true;
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "新增/修改销售单信息出错！[" + ex.ToString() + "]", EMenuList.销售管理);
            }
            db.Close();
            if (ioldId == 0)
            {
                LinkService.InsertLog(ESysLogType.Operation, "新增销售单信息[" + ValueHandler.GetStringValue(SM.SBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.销售管理);
            }
            else
            {
                LinkService.InsertLog(ESysLogType.Operation, "修改销售单信息[" + ValueHandler.GetStringValue(SM.SBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.销售管理);
            }
            return blRes;
        }
        /// <summary>
        /// 校验库存是否满足
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="dtDetail"></param>
        /// <returns></returns>
        public List<string> CheckStore(object obj, DataTable dtDetail)
        { 
            SaleBill SM = obj as SaleBill;
            int ioldId = SM.SBill_Id;
            List<string> LSProduct = new List<string>();
            try
            {
          
                StringBuilder sbSql = new StringBuilder();
                if (SM.SBill_Id == 0)
                {
                   //如果该销售单是新增单据，那么直接校验该表中的数据在库存中是否满足
                    for(int i = 0 ; i<dtDetail.Rows.Count;i++)
                    {
                        DataBase _db = new DataBase();
                        StringBuilder _sbSql = new StringBuilder();
                        _sbSql.Append(" SELECT * FROM StockDetail WHERE SDeta_Stock_Id=1 AND SDeta_RealNumber>=" + ValueHandler.GetIntNumberValue(dtDetail.Rows[i]["SBDeta_Count"]) + " AND SDeta_PInfo_Id=" + ValueHandler.GetIntNumberValue(dtDetail.Rows[i]["SBDeta_PInfo_Id"]));
                        SqlHelper.FillDataset(_db.Connection, CommandType.Text, _sbSql.ToString(), _db.Data, new string[] { "dtList"});
                        if (_db.Data.Tables["dtList"].Rows.Count <= 0)
                        {
                            LSProduct.Add(ValueHandler.GetStringValue(dtDetail.Rows[i]["PInfo_Name"]));//库存不足的产品名称
                        }
                    }
                }
                else
                { 
                    //如果是编辑销售单，那么修改后的销售单中的产品数量<=修改前的产品数量+库存数量，则通过校验，否则不能通过校验
                    //1)先取出该销售单之前的数据（当前数据库中的数据)
                    DataTable dtDataBaseDetail = this.GetDetail(SM);//
                    //2)校验当前内存中的数据
                    //如果该销售单是新增单据，那么直接校验该表中的数据在库存中是否满足: 库存量>=修改后-修改前 ，则满足
                    for (int i = 0; i < dtDetail.Rows.Count; i++)
                    {
                        DataBase _db = new DataBase();
                        StringBuilder _sbSql = new StringBuilder();
                        DataView dv = new DataView(dtDataBaseDetail);
                        dv.RowFilter = "SBDeta_PInfo_Id=" + ValueHandler.GetIntNumberValue(dtDetail.Rows[i]["SBDeta_PInfo_Id"]).ToString();
                        decimal decBeforeCount = 0;
                        if (dv.Count > 0)
                            decBeforeCount = ValueHandler.GetDecNumberValue(dv[0]["SBDeta_Count"]);
                        _sbSql.Append(" SELECT * FROM StockDetail WHERE SDeta_Stock_Id=1 AND SDeta_RealNumber>=" + (ValueHandler.GetDecNumberValue(dtDetail.Rows[i]["SBDeta_Count"])-decBeforeCount ).ToString()+ " AND SDeta_PInfo_Id=" + ValueHandler.GetIntNumberValue(dtDetail.Rows[i]["SBDeta_PInfo_Id"]));
                        SqlHelper.FillDataset(_db.Connection, CommandType.Text, _sbSql.ToString(), _db.Data, new string[] { "dtList" });
                        if (_db.Data.Tables["dtList"].Rows.Count <= 0)
                        {
                            LSProduct.Add(ValueHandler.GetStringValue(dtDetail.Rows[i]["PInfo_Name"]));//库存不足的产品名称
                        }
                    }
                }
              
              
            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "校验库存信息出错！[" + ex.ToString() + "]", EMenuList.销售管理);
            }
            db.Close();
            return LSProduct;
        }
        public override bool HasExists(object obj)
        {
            SaleBill SM = obj as SaleBill;
            bool blRes = true;
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" SELECT * FROM SaleBill WHERE SBill_Code='" + SM.SBill_Code + "' AND SBill_Id<>" + SM.SBill_Id + " AND State=1 ");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                    blRes = true;
                else
                    blRes = false;

            }
            catch (Exception ex)
            {
            }
            db.Close();
            return blRes;
        }

        public override bool Delete(object obj)
        {
            DataBase db = new DataBase();
            bool blRes = false;
            try
            {
                db.Open();
                SaleBill SM = obj as SaleBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(" UPDATE SaleBill SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE SBill_Id=" + SM.SBill_Id);
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                //还原库存信息
                sbSql = new StringBuilder();
                sbSql.Append(" SELECT SBDeta_Id  ,SBDeta_SBill_Id,SBDeta_PInfo_Id,SBDeta_Count,SBDeta_Price,SBDeta_Money,SBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM SaleBillDetail A ");
                sbSql.Append(" LEFT JOIN ProductInfo B ON (A.SBDeta_PInfo_Id=B.PInfo_Id) ");
                sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                sbSql.Append(" WHERE A.SBDeta_SBill_Id=" + SM.SBill_Id + " AND A.State=1");
                SqlHelper.FillDataset(db.Transaction, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                for (int i = 0; i < db.Data.Tables[0].Rows.Count; i++)
                {
                    sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE StockDetail SET SDeta_RealNumber=SDeta_RealNumber+" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " , SDeta_OutputNumber=SDeta_OutputNumber-" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_Count"]) + " WHERE SDeta_Stock_Id=1 AND SDeta_PInfo_Id=" + ValueHandler.GetStringValue(db.Data.Tables[0].Rows[i]["SBDeta_PInfo_Id"]));
                    SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());
                }
                //删除库存信息
                sbSql = new StringBuilder();
                sbSql.Append(" UPDATE SaleBillDetail SET ");
                sbSql.Append(" State=0 ");
                sbSql.Append(" WHERE SBDeta_SBill_Id=" + SM.SBill_Id);
                SqlHelper.ExecuteNonQuery(db.Transaction, CommandType.Text, sbSql.ToString());               
                
                db.Transaction.Commit();
                LinkService.InsertLog(ESysLogType.Operation, "删除销售单信息[" + ValueHandler.GetStringValue(SM.SBill_Code, ValueHandler.CharHandlerType.IsRepChar) + "]！", EMenuList.销售管理);
                blRes = true;

            }
            catch (Exception ex)
            {
                db.Transaction.Rollback();
                LinkService.InsertLog(ESysLogType.Error, "删除销售单信息！[" + ex.ToString() + "]", EMenuList.销售管理);
            }
            db.Close();
            return blRes; ;
        }
        public DataTable GetDetail(object obj)
        {
            try
            {
                db = new DataBase();
                SaleBill SM = obj as SaleBill;
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id,SBill_Code,SBill_Code2,SBill_Cust_Id,SBill_TaxMoney,SBill_Money,SBill_RealMoney,SBill_PayType,SBill_AList_Id,SBill_Date,SBill_User,B.SUser_Name,SBill_Type,SBill_State,A.State,A.CreateTime,A.CreateMan ,SBill_Remark,C.Cust_Name,D.AList_Name,E.BData_Name AS SBill_TypeName,SBill_PayType,CASE SBill_PayType WHEN 1 THEN '现金 ' ELSE ' 刷卡' END AS SBill_PayTypeName ,SBill_State,
CASE SBill_State WHEN 0 THEN '未结算' ELSE '已结算' END AS SBill_StateName FROM SaleBill A LEFT JOIN SysUser B ON (A.SBill_User=B.SUser_Id) LEFT JOIN Customer C ON (C.Cust_Id = A.SBill_Cust_Id) LEFT JOIN AccountList D ON (D.AList_Id = A.SBill_AList_Id) LEFT JOIN BaseData E ON (E.BData_Id=A.SBill_Type) ");
                sbSql.Append(" WHERE SBill_Id=" + SM.SBill_Id);
                 
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.SBill_AList_Id = ValueHandler.GetIntNumberValue(dr["SBill_AList_Id"]);
                    SM.SBill_Code = ValueHandler.GetStringValue(dr["SBill_Code"]);
                    SM.SBill_Code2 = ValueHandler.GetStringValue(dr["SBill_Code2"]);
                    SM.SBill_Cust_Id = ValueHandler.GetIntNumberValue(dr["SBill_Cust_Id"]);
                    SM.SBill_Date = DateTime.Parse(dr["SBill_Date"].ToString());
                    SM.SBill_Id = ValueHandler.GetIntNumberValue(dr["SBill_Id"]);
                    SM.SBill_Money = ValueHandler.GetDecNumberValue(dr["SBill_Money"]);
                    SM.SBill_PayType = ValueHandler.GetIntNumberValue(dr["SBill_PayType"]);
                    SM.SBill_RealMoney = ValueHandler.GetDecNumberValue(dr["SBill_RealMoney"]);
                    SM.SBill_Remark = ValueHandler.GetStringValue(dr["SBill_Remark"]);
                    SM.SBill_State = ValueHandler.GetIntNumberValue(dr["SBill_State"]);
                    SM.SBill_TaxMoney = ValueHandler.GetDecNumberValue(dr["SBill_TaxMoney"]);
                    SM.SBill_Type = ValueHandler.GetIntNumberValue(dr["SBill_Type"]);
                    SM.SBill_User = ValueHandler.GetIntNumberValue(dr["SBill_User"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                }
                db.Close();
                db = new DataBase();
                sbSql = new StringBuilder();
                sbSql.Append(" SELECT SBDeta_Id  ,SBDeta_SBill_Id,SBDeta_PInfo_Id,SBDeta_Count,SBDeta_Price,SBDeta_Money,SBDeta_Memo ,PInfo_Code,PInfo_Name,PInfo_PSort_ID ,C.PSort_Name FROM SaleBillDetail A ");
                sbSql.Append(" LEFT JOIN ProductInfo B ON (A.SBDeta_PInfo_Id=B.PInfo_Id) ");
                sbSql.Append(" LEFT JOIN ProductSort C ON (B.PInfo_PSort_ID=C.PSort_ID) ");
                sbSql.Append(" WHERE A.SBDeta_SBill_Id=" + SM.SBill_Id + " AND A.State=1");
                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                db.Data.Tables[0].Columns["SBDeta_Id"].AutoIncrement = true;
                db.Data.Tables[0].Columns["SBDeta_Id"].AutoIncrementStep = 1;
                if (db.Data.Tables[0].Rows.Count <= 0)
                    db.Data.Tables[0].Columns["SBDeta_Id"].AutoIncrementSeed = 1;
                else
                    db.Data.Tables[0].Columns["SBDeta_Id"].AutoIncrementSeed = ValueHandler.GetIntNumberValue(db.Data.Tables[0].Compute("max(SBDeta_Id)+1", ""));

            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货列表加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }
        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id,SBill_Code,SBill_Code2,SBill_Cust_Id,SBill_TaxMoney,SBill_Money,SBill_RealMoney,SBill_PayType,SBill_AList_Id,SBill_Date,SBill_User,B.SUser_Name,SBill_Type,SBill_State,A.State,A.CreateTime,A.CreateMan ,SBill_Remark,C.Cust_Name,D.AList_Name,E.BData_Name AS SBill_TypeName,SBill_PayType,CASE SBill_PayType WHEN 1 THEN '现金 ' ELSE ' 刷卡' END AS SBill_PayTypeName ,SBill_State,
CASE SBill_State WHEN 0 THEN '未结算' ELSE '已结算' END AS SBill_StateName FROM SaleBill A LEFT JOIN SysUser B ON (A.SBill_User=B.SUser_Id) LEFT JOIN Customer C ON (C.Cust_Id = A.SBill_Cust_Id) LEFT JOIN AccountList D ON (D.AList_Id = A.SBill_AList_Id) LEFT JOIN BaseData E ON (E.BData_Id=A.SBill_Type) ");
                sbSql.Append(" WHERE A.State=1 ");
                if (!string.IsNullOrEmpty(strCondition))
                {
                    sbSql.Append(" AND " + strCondition);
                }
                sbSql.Append(" ORDER BY A.CreateTime DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "销售单列表加载失败！[" + ex.ToString() + "]", EMenuList.销售管理);
            }
            db.Close();
            return db.Data.Tables[0];
        }

        public override object GetModel(object obj)
        {
            SaleBill SM = obj as SaleBill;

            try
            {
                db = new DataBase();
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id,SBill_Code,SBill_Code2,SBill_Cust_Id,SBill_TaxMoney,SBill_Money,SBill_RealMoney,SBill_PayType,SBill_AList_Id,SBill_Date,SBill_User,B.SUser_Name,SBill_Type,SBill_State,A.State,A.CreateTime,A.CreateMan ,C.Cust_Name,D.AList_Name,E.BData_Name AS SBill_TypeName,SBill_PayType,CASE SBill_PayType WHEN 1 THEN '现金 ' ELSE ' 刷卡' END AS SBill_PayTypeName ,SBill_State,
CASE SBill_State WHEN 0 THEN '未结算' ELSE '已结算' END AS SBill_StateName,SBill_Remark FROM SaleBill A LEFT JOIN SysUser B ON (A.SBill_User=B.SUser_Id) LEFT JOIN Customer C ON (C.Cust_Id = A.SBill_Cust_Id) LEFT JOIN AccountList D ON (D.AList_Id = A.SBill_AList_Id) LEFT JOIN BaseData E ON (E.BData_Id=A.SBill_Type) ");
                sbSql.Append(" WHERE SBill_Id=" + SM.SBill_Id);

                SqlHelper.FillDataset(db.Connection, CommandType.Text, sbSql.ToString(), db.Data, new string[] { "dtList" });
                if (db.Data.Tables[0].Rows.Count > 0)
                {
                    DataRow dr = db.Data.Tables[0].Rows[0];
                    SM.CreateMan = ValueHandler.GetIntNumberValue(dr["CreateMan"]);
                    SM.CreateTime = DateTime.Parse(dr["CreateTime"].ToString());
                    SM.SBill_AList_Id = ValueHandler.GetIntNumberValue(dr["SBill_AList_Id"]);
                    SM.SBill_Code = ValueHandler.GetStringValue(dr["SBill_Code"]);
                    SM.SBill_Code2 = ValueHandler.GetStringValue(dr["SBill_Code2"]);
                    SM.SBill_Cust_Id = ValueHandler.GetIntNumberValue(dr["SBill_Cust_Id"]);
                    SM.SBill_Date = DateTime.Parse(dr["SBill_Date"].ToString());
                    SM.SBill_Id = ValueHandler.GetIntNumberValue(dr["SBill_Id"]);
                    SM.SBill_Money = ValueHandler.GetDecNumberValue(dr["SBill_Money"]);
                    SM.SBill_PayType = ValueHandler.GetIntNumberValue(dr["SBill_PayType"]);
                    SM.SBill_RealMoney = ValueHandler.GetDecNumberValue(dr["SBill_RealMoney"]);
                    SM.SBill_Remark = ValueHandler.GetStringValue(dr["SBill_Remark"]);
                    SM.SBill_State = ValueHandler.GetIntNumberValue(dr["SBill_State"]);
                    SM.SBill_TaxMoney = ValueHandler.GetDecNumberValue(dr["SBill_TaxMoney"]);
                    SM.SBill_Type = ValueHandler.GetIntNumberValue(dr["SBill_Type"]);
                    SM.SBill_User = ValueHandler.GetIntNumberValue(dr["SBill_User"]);
                    SM.State = ValueHandler.GetIntNumberValue(dr["State"]);
                } 
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "进货列表加载失败！[" + ex.ToString() + "]", EMenuList.进货单管理);
            }
            db.Close();
            return SM;
        }
        public bool JS(object obj)
        {
            SaleBill SModel = obj as SaleBill;
            AccountRecord SM = new AccountRecord();
            SM.AReco_Code = Coder.CreateCoder(EMenuList.转账管理);
            SM.AReco_Datetime = DateTime.Parse( System.DateTime.Now.ToString("yyyy-MM-dd"));
            SM.AReco_InNumber = SModel.SBill_AList_Id;
            SM.AReco_Kind = 2;
            SM.AReco_Man = LinkService.GetCurrentUser().SUser_Id;
            SM.AReco_Noney = SModel.SBill_RealMoney;
            SM.AReco_OutNumber = -1;
            SM.AReco_Reason = "销售单[" + SModel.SBill_Code + "]结算，结算金额：" + SModel.SBill_RealMoney;
            SM.AReco_Type = 1;
            SM.CreateMan = LinkService.GetCurrentUser().SUser_Id;
            SM.CreateTime = System.DateTime.Now;
            SM.State = 1;
            AccountRecordOp accountRecordManager = new AccountRecordOp();
            bool blRes = accountRecordManager.Save(SM);
            if (blRes)
            {
                try
                {
                    StringBuilder sbSql = new StringBuilder();
                    sbSql.Append(" UPDATE SaleBill SET SBill_State = 1 , SBill_ARecoId=" + SM.ARecoId + " WHERE SBill_Id=" + SModel.SBill_Id);
                    db.Open();
                    SqlHelper.ExecuteNonQuery(db.Connection, CommandType.Text, sbSql.ToString());
                    blRes = true;
                    LinkService.InsertLog(ESysLogType.Operation, "销售单["+SModel.SBill_Code+"]结算完成！", EMenuList.销售管理);
                }
                catch (Exception ex)
                {
                    blRes = false;
                }
                db.Close();
            }
            return blRes;
        }
    }
}
